
function onAddNewCategory(frmID, msgDiv, cattable, cbo)
{
	var newId = '';
	
	$(frmID).request(
		{
			asynchronous: false,
			parameters: $(frmID).serialize(true),
	  		onComplete: function(transport)
	  		{ 
			  	var response = transport.responseText || "";
		    	
		    	$(msgDiv).innerHTML = '';
		    	//alert(response);
		    	if(response != "")
		    	{
		    		obj = response.evalJSON();
		    		
		    		
		    		if(obj == null)
		    		{
		    			$(msgDiv).update('Something wrong! Reponsed string is: ' + response);
		    		}
		    		
			    	if(msgDiv != null)
				    	$(msgDiv).update(obj.message);
				    
				    if(obj.ID > -1)
					{
					    if(cattable != null && $(cattable) != null)
					    {
							sClassName = 'newrow';
						
						    	//repair new row used to add to table
							    str = '<tr id="cat-' + obj.ID + '" class="' + sClassName + '">';
								str += '<th scope="row" style="text-align: center">'
										+ obj.ID + '</th>';
								str += '<td>' + obj.new_name + '</td>';
								str += '<td>' + obj.cat_description + '</td>';
								str += '<td><a href="?mod=manage&sub1=editcategory&id=' + obj.ID + '" class="edit">Edit</td>';
								str += '<td><a onclick="onDeleteCat(\'' + 
								$(frmID).action + '\',\'' + obj.ID + '\', \'cat_parent\')" class="delete">Delete</td>';
								str += '</tr>';
								
								$(cattable).innerHTML += str;
								
								newId = 'cat-' + obj.ID;
					    }
					    
					    if(cbo != null)
						{
							$(cbo).innerHTML += '<option id="' + cbo + '-' + obj.ID 
												+ '" value="' + obj.ID + '">'
												+ obj.new_name + '</option>';
						}
		    		}
		    	}
	  		}
		});

	$(msgDiv).style.display = 'block';
	Fat.fade_element(msgDiv, null, 1500, null, null);
	if(newId != '')
	{
		Fat.fade_element(newId);
		$(frmID).reset();
	}
	//revent submit page
	return false;
}

function onDeleteCat(url, catId, cbo,parent,position)
{
	var isOK = -1;
	
	if(!confirm('Are you sure you want to delete the category with Id:' + catId))
	{
		return false;
	}
	
	new Ajax.Request(url, 
	{
	  method: 'post',
	  asynchronous: false,
	  parameters:'act=delcat&id=' + catId + '&cat_position='+position+ '&catparent='+parent,
	  onSuccess: function(transport) 
  		{
			var response = transport.responseText.trim();
			
			//alert(response);
			
			obj = response.evalJSON();

    		if(obj == null)
    		{
    			$('ajaxResponse1').innerHTML = 'Something wrong! Reponsed string is: ' + response;
    		}
    		
    		isOK = obj.code;
		
    		$('ajaxResponse1').update(obj.message);
		}
	});

	$('ajaxResponse1').style.display = 'inherit';
	Fat.fade_element('ajaxResponse1', null, 1500, null, null);

	if(isOK == 0){
		rowID = cbo + '-' + catId;
		Fat.fade_element(rowID, null, 1000, "#f22", null);
		setTimeout("$(rowID).remove()", 1000);
		
	}				
}


function onEditCat(frmID, msgDiv)
{
	var newId = '';
	var status = null;
	var ret = null;
	var message = null;
	$(frmID).request(
		{
			asynchronous: false,
			parameters: $(frmID).serialize(true),
	  		onComplete: function(transport)
	  		{ 
			  	var response = transport.responseText || "";
				//alert(response);
		    	$(msgDiv).innerHTML = '';
		    	if(response != "")
		    	{
		    		obj = response.evalJSON();
		    		if(obj == null)
		    		{
		    			$(msgDiv).update('Something wrong! Reponsed string is: ' + response);
		    		}
		    		
			    	if(msgDiv != null)
				    	$(msgDiv).update(obj.message);
					if(obj.ID==0)
					{
						ret =1;	
						message = obj.msg;
					}
					
		    	}
	  		}
		});
	if(ret==1)
	{
		document.location = '?mod=manage&sub1=category&message=' + message;
	}
	else
	{
	$(msgDiv).style.display = 'block';
	Fat.fade_element(msgDiv, null, 1500, null, null);
	//revent submit page
	}
	return false;
}
